package org.study.bookmanager.controller;


import jakarta.annotation.Resource;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.*;
import org.study.bookmanager.io.reponse.RestBean;
import org.study.bookmanager.io.request.PageReq;
import org.study.bookmanager.service.ReservationService;

@RestController
@RequestMapping(value = "/v1/reservation",produces = "application/json;charset=UTF-8")
public class ReservationController {
    @Resource
    private ReservationService reservationService;


    @PostMapping("/{bookId}")
    String addReservation(@PathVariable int bookId){
        UserDetails userDetails = (UserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        Object resp=reservationService.addReservation(userDetails,bookId);
        if(RestBean.handlerError(resp)!=null){
            return RestBean.handlerError(resp);
        }
        return RestBean.success(resp);

    }

    @DeleteMapping("/{Id}")
    String deleteReservation(@PathVariable int Id){
        UserDetails userDetails = (UserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        Object resp=reservationService.deleteReservation(userDetails,Id);
        if(RestBean.handlerError(resp)!=null){
            return RestBean.handlerError(resp);
        }
        return RestBean.success(resp);
    }

    @GetMapping("/list")
    String getReservations(@RequestBody PageReq pageReq){
        UserDetails userDetails = (UserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        Object resp=reservationService.getReservations(userDetails,pageReq);
        if(RestBean.handlerError(resp)!=null){
            return RestBean.handlerError(resp);
        }
        return RestBean.success(resp);
    }
}
